;
; Copyright 2010 - 2011, The PLDesktop Development Team
;
; This library is free software; you can redistribute it and/or
; modify it under the terms of the GNU Lesser General Public
; License as published by the Free Software Foundation; either
; version 2.1 of the License, or (at your option) any later version.
;
; This library is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; Lesser General Public License for more details.
;
; You should have received a copy of the GNU Lesser General Public
; License along with this library; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;
;

; InterruptManager::interrupt(InterruptState &interruptState)
extern _ZN18PLInterruptManager9interruptER14InterruptState
; Export the array of interrupt handler addresses
global interrupt_handler_array

[bits 32]
[section .text]
interrupt_handler:
	; Save the registers
	pusha
	push ds
	; Switch to the kernel data segment
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	; Create a new stackframe
	xor ebp, ebp
	; Push the pointer to the InterruptState object
	mov eax, esp
	push eax
	; Call the C++ Code
	call _ZN18PLInterruptManager9interruptER14InterruptState
	; Cleanup the stack
	add esp, 0x04
	; Restore the registers
	pop ds
	mov ax, ds
	mov es, ax
	popa
	; Remove the errorcode and the interrupt number from the stack
	add esp, 0x08
	iret

%macro INTERRUPT_HANDLER 1
interrupt%1:
	; Push a fake error code
	push dword 0
	; Push the interrupt number
	push dword %1
	jmp interrupt_handler
%endmacro

%macro INTERRUPT_HANDLER_ERRORCODE 1
interrupt%1:
	; Push the interrupt number
	push dword %1
	jmp interrupt_handler
%endmacro

INTERRUPT_HANDLER 0
INTERRUPT_HANDLER 1
INTERRUPT_HANDLER 2
INTERRUPT_HANDLER 3
INTERRUPT_HANDLER 4
INTERRUPT_HANDLER 5
INTERRUPT_HANDLER 6
INTERRUPT_HANDLER 7
INTERRUPT_HANDLER_ERRORCODE 8
INTERRUPT_HANDLER 9
INTERRUPT_HANDLER_ERRORCODE 10
INTERRUPT_HANDLER_ERRORCODE 11
INTERRUPT_HANDLER_ERRORCODE 12
INTERRUPT_HANDLER_ERRORCODE 13
INTERRUPT_HANDLER_ERRORCODE 14
INTERRUPT_HANDLER 15
INTERRUPT_HANDLER 16
INTERRUPT_HANDLER_ERRORCODE 17
INTERRUPT_HANDLER 18
INTERRUPT_HANDLER 19
INTERRUPT_HANDLER 20
INTERRUPT_HANDLER 21
INTERRUPT_HANDLER 22
INTERRUPT_HANDLER 23
INTERRUPT_HANDLER 24
INTERRUPT_HANDLER 25
INTERRUPT_HANDLER 26
INTERRUPT_HANDLER 27
INTERRUPT_HANDLER 28
INTERRUPT_HANDLER 29
INTERRUPT_HANDLER 30
INTERRUPT_HANDLER 31

%assign i 32
%rep 224
	INTERRUPT_HANDLER i
	%assign i i+1
%endrep

[section .data]
interrupt_handler_array:
	%assign i 0
	%rep 256
		dd interrupt %+ i
		%assign i i+1
	%endrep
